package promote.brozen.dubbo.consumers.annotationconfig;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import promote.brozen.dubbo.interfaces.api.PermissionApi;
import promote.brozen.dubbo.interfaces.api.UserApi;
import promote.brozen.dubbo.interfaces.beans.User;

/**
 * @author Brozen
 * @date 2020/9/9 7:43 PM
 * @email brozen@qq.com
 */
@SpringBootApplication
@EnableDubbo(scanBasePackages = "promote.brozen.dubbo.consumers")
public class ConsumerApplication {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private PermissionApi permissionApi;

    public static void main(String[] args) {
        ConfigurableApplicationContext ac = new SpringApplicationBuilder()
                .web(WebApplicationType.NONE)
                .sources(ConsumerApplication.class)
                .build()
                .run(args);

        ConsumerApplication app = ac.getBean(ConsumerApplication.class);

        User user = new User();
        user.setMail("1bb@qq.com");
        user.setNick("1bb");
        user.setPassword("1bbpp");
        app.userApi.createUser(user);

        System.out.println(app.userApi.listUser());
    }

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("TestExportApiWithJavaConfig");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("nacos://127.0.0.1:8848?namespace=484a0ac6-add3-4ea8-847d-cdefec40f5b8");
        registryConfig.setProtocol("nacos");
        registryConfig.setGroup("dev");
        return registryConfig;
    }

}
